import tkinter as tk
import os
import subprocess
from tkinter import ttk
from tkinter import Menu
import tkinter.font as tkFont
from tkinter import messagebox

class PyWinDesign:
    def __init__(self, 启动窗口):
        self.启动窗口 = 启动窗口
        self.启动窗口.title("启动窗口")
        self.启动窗口.resizable(width=False, height=False)
        screenwidth = self.启动窗口.winfo_screenwidth()
        screenheight = self.启动窗口.winfo_screenheight()
        size = '%dx%d+%d+%d' % (333, 210, (screenwidth -333) / 2, (screenheight - 210) / 2)
        self.启动窗口.geometry(size)

        self.标签1 = tk.StringVar()
        self.标签1.set('标签')
        self.标签1 = tk.Label(self.启动窗口, textvariable=self.标签1, anchor=tk.W)
        self.标签1.place(x=20, y=10, width=80, height=24)

        self.按钮1 = tk.StringVar()
        self.按钮1.set('按钮1')
        self.按钮1 = tk.Button(self.启动窗口, textvariable=self.按钮1, command=self.hi)
        self.按钮1.place(x=46, y=68, width=80, height=32)
        self.BeautifyButton(self.按钮1)

        self.按钮2 = tk.StringVar()
        self.按钮2.set('按钮2')
        self.按钮2 = tk.Button(
            self.启动窗口, textvariable=self.按钮2, command=self.ping)
        self.按钮2.place(x=206, y=68, width=80, height=32)
        self.BeautifyButton(self.按钮2)

        self.编辑框1 = tk.Text(self.启动窗口, wrap=tk.NONE)
        self.编辑框1.insert(tk.END, '零基础入门学习Python')
        self.编辑框1.place(x=100, y=13, width=155, height=20)

        self.组合框1 = ttk.Combobox(self.启动窗口, values=(
            '1', '2', '3', '4', '5'), state='readonly')
        self.组合框1.current(0)
        self.组合框1.place(x=100, y=40, width=100, height=20)

    def ping(self):
        #堵塞方法执行cmd命令
        #print(os.popen("ping 127.0.0.1").read())
        #不堵塞执行cmd命令
        print(subprocess.Popen("ping 127.0.0.1").stdout.read())
        


    def hi(self):
        #确实提示框
        tk.messagebox.showinfo(self.启动窗口.title() + " - 提示", message="内容")
        #是否提示框，选择是返回tk.YES，选择否返回tk.NO
        tk.messagebox.askquestion(title=self.启动窗口.title() + " - 提示", message="内容")

    def BeautifyButton(self, Button):
        Button.config(bg="#0083EE",
                      fg="white",
                      activeforeground="white",
                      activebackground="#0088FF",
                      bd=0)
        Button.bind('<Enter>', lambda self: Button.config(bg="#0F93FF"))
        Button.bind('<Leave>', lambda self: Button.config(bg="#0083EE"))


if __name__ == '__main__':
    root = tk.Tk()
    default_font = tkFont.Font(family='微软雅黑', size=9)
    root.option_add("*Font", default_font)

    menubar = Menu(root)
    
    菜单1 = Menu(menubar, tearoff=False)
    菜单1.add_command(label="菜单项1")
    菜单1.add_separator()
    菜单1.add_command(label="菜单项2")
    menubar.add_cascade(label="菜单1", menu=菜单1)

    单选菜单 = Menu(menubar, tearoff=False)
    单选菜单.add_radiobutton(label="菜单项1")
    单选菜单.add_radiobutton(label="菜单项2")
    menubar.add_cascade(label="单选菜单", menu=单选菜单)

    多选菜单 = Menu(menubar, tearoff=False)
    多选菜单.add_checkbutton(label="菜单项1")
    多选菜单.add_checkbutton(label="菜单项2")
    menubar.add_cascade(label="多选菜单", menu=多选菜单)

    root.config(menu=menubar)
    # 绑定右键菜单
    root.bind("<Button-3>", lambda event: 菜单1.post(event.x_root, event.y_root))

    root.iconbitmap("Icon.ico")
    app = PyWinDesign(root)
    root.mainloop()
